﻿Public Class SearchBox
    Private _grvResources As DataGridView
    Private _tableName As String

    Private _resultDA As DataTable
    Private _resourcesDA As DataTable

    Private _map As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer)
    Private _curRowResultSelected As Integer = -1

    Public Sub New(ByVal grvResources As DataGridView, ByVal tableName As String)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me._grvResources = grvResources
        Me._tableName = tableName

        Me._resourcesDA = Me._grvResources.DataSource
        If (Me._resourcesDA Is Nothing) Then
            Return
        End If
        Me._resultDA = Me._resourcesDA.Clone

        Dim poInfo As POInfo = PO.poInfoAll.Item(Me._tableName)
        Dim i As Integer = 0
        For i = 0 To poInfo.columnCount - 1
            Me.cbComlumnName.Items.Add(poInfo.getPOInfoColumn(i).getColumnName())
        Next
        Me.cbComlumnName.Text = Me.cbComlumnName.Items(0).ToString

    End Sub


    Private Sub txtKeySearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtKeySearch.KeyPress

        If (Me._resourcesDA Is Nothing) Then
            Return
        End If

        If Not e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            Return
        End If

        Me._resultDA.Rows.Clear()
        Me._map.Clear()

        If (txtKeySearch.Text Is Nothing Or txtKeySearch.Text.Equals("")) Then
            Dim j As Integer = 0
            For j = 0 To Me._resourcesDA.Rows.Count - 1
                Me._resultDA.ImportRow(Me._resourcesDA.Rows(j))
                Me._map.Add(j, j)
            Next

            Me.grvResult.DataSource = Me._resultDA

            Return
        End If

        Dim i As Integer = 0
        Dim count As Integer = -1

        For i = 0 To Me._resourcesDA.Rows.Count - 1
            If (Me._resourcesDA.Rows(i)(Me.cbComlumnName.Text) = Me.txtKeySearch.Text) Then
                Me._resultDA.ImportRow(Me._resourcesDA.Rows(i))
                count = count + 1
                Me._map.Add(count, i)
            End If
        Next

        Me.grvResult.DataSource = Me._resultDA

    End Sub

    Private Sub grvResult_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grvResult.RowHeaderMouseClick
        Me._curRowResultSelected = e.RowIndex
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If (Me._resultDA Is Nothing) Then
            Return
        End If

        If (Me._curRowResultSelected < 0 Or Me._curRowResultSelected > (Me._resultDA.Rows.Count - 1)) Then
            Return
        End If

        Me._grvResources.ClearSelection()
        Me._grvResources.Rows(Me._map.Item(Me._curRowResultSelected)).Selected = True

        Me.Close()

    End Sub
End Class